package com.grj.park.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.grj.park.common.R;
import com.grj.park.domain.Contracts;
import com.grj.park.domain.Customers;
import com.grj.park.service.ContractsService;
import com.grj.park.service.CustomersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("customers")
public class CustomersController {

    private final CustomersService customersService;

    @GetMapping
    public R<List<Customers>> getCustomers(){
        return R.ok(customersService.list());
    }

    @PostMapping
    public R<?> saveCustomers(@RequestBody Customers customers){
        LambdaQueryWrapper<Customers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Customers::getCustomerName, customers.getCustomerName());
        Customers one = customersService.getOne(queryWrapper);
        if (one != null){
            return R.fail("客户名字已存在");
        }
        customersService.save(customers);
        return R.okMsg("添加客户成功");
    }
}
